<template>
  <div class="dev-status-management">
    <div class="page-title">设备状态报警信息</div>
    
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :model="searchForm" :inline="true">
        <el-form-item label="设备编码">
          <el-input v-model="searchForm.devCode" placeholder="请输入设备编码" clearable />
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input v-model="searchForm.devName" placeholder="请输入设备名称" clearable />
        </el-form-item>
        <el-form-item label="设备状态">
          <el-select v-model="searchForm.devStatusValue" placeholder="请选择设备状态" style="width: 150px" clearable>
            <el-option label="正常" value="正常" />
            <el-option label="报警" value="报警" />
            <el-option label="故障" value="故障" />
            <el-option label="停机" value="停机" />
          </el-select>
        </el-form-item>
        <el-form-item label="报警时间">
          <el-date-picker
            v-model="searchForm.createTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
            style="width: 240px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon> 查询
          </el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card>
      <el-table :data="devStatusList" stripe v-loading="loading">
        <el-table-column prop="id" label="ID" width="80" v-if="false"/>
        <el-table-column prop="devCode" label="设备编码" />
        <el-table-column prop="devName" label="设备名称" />
        <el-table-column prop="devStatusValue" label="设备状态">
          <template #default="scope">
            <el-tag :type="getStatusTagType(scope.row.devStatusValue)">
              {{ scope.row.devStatusValue }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="devError" label="设备报警信息" show-overflow-tooltip />
        <el-table-column prop="createTime" label="报警时间">
          <template #default="scope">
            {{ formatDateTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="scope">
            <el-button size="small" type="primary" @click="handleDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.current"
          v-model:page-size="pagination.size"
          :total="pagination.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    
    <!-- 设备状态报警信息详情弹窗 -->
    <DevStatusForm
      :visible="detailVisible"
      @update:visible="detailVisible = $event"
      :form-data="detailData"
      @close="handleDetailClose" />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { request } from '@/utils/request'
import { ElMessage } from 'element-plus'

// 导入组件
import DevStatusForm from './DevStatusForm.vue'

// 响应式数据
const loading = ref(false)
const devStatusList = ref([])
const detailVisible = ref(false)

// 搜索表单
const searchForm = reactive({
  devCode: '',
  devName: '',
  devStatusValue: '',
  createTime: []
})

// 分页
const pagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

// 详情数据
const detailData = reactive({
  id: '',
  devCode: '',
  devName: '',
  devStatusValue: '',
  devError: '',
  createTime: ''
})

// 状态标签类型
const getStatusTagType = (status) => {
  const statusMap = {
    '正常': 'success',
    '报警': 'warning',
    '故障': 'danger',
    '停机': 'info'
  }
  return statusMap[status] || ''
}

// 日期时间格式化
const formatDateTime = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

// 加载设备状态报警数据
const loadDevStatusData = async () => {
  loading.value = true
  try {
    const params = {
      filter: {
        pageIndex: pagination.current,
        pageSize: pagination.size,
        orderBy: "CREATETIME",
        isAscending: false // 默认按报警时间倒序排列
      },
      devStatus: {
        devCode: searchForm.devCode,
        devName: searchForm.devName,
        devStatusValue: searchForm.devStatusValue,
        createTime: searchForm.createTime && searchForm.createTime.length > 0 ? searchForm.createTime[0] : null
      }
    }
    
    // 调用后端API
    const response = await request.post('/DevStatus/DevStatusPage', params)
    devStatusList.value = response.data
    pagination.total = response.totalCount
  } catch (error) {
    ElMessage.error('加载设备状态报警信息失败')
    console.error('加载设备状态报警信息失败:', error)
  } finally {
    loading.value = false
  }
}

// 查询
const handleSearch = () => {
  pagination.current = 1
  loadDevStatusData()
}

// 重置
const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    if (key === 'createTime') {
      searchForm[key] = []
    } else {
      searchForm[key] = ''
    }
  })
  pagination.current = 1
  loadDevStatusData()
}

// 查看详情
const handleDetail = (row) => {
  // 填充详情数据
  Object.keys(detailData).forEach(key => {
    if (row[key] !== undefined && row[key] !== null) {
      detailData[key] = row[key]
    }
  })
  
  detailVisible.value = true
}

// 详情关闭处理
const handleDetailClose = () => {
  // 可以在这里添加关闭后的清理逻辑
}

// 分页大小改变
const handleSizeChange = (size) => {
  pagination.size = size
  pagination.current = 1
  loadDevStatusData()
}

// 当前页改变
const handleCurrentChange = (page) => {
  pagination.current = page
  loadDevStatusData()
}

// 页面加载时获取数据
onMounted(() => {
  loadDevStatusData()
})
</script>

<style scoped>
.dev-status-management {
  padding: 20px;
}

.page-title {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 20px;
  color: #303133;
}

.search-card {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  text-align: center;
}
</style>